import os
import sys
from xml.dom import minidom
import socf.services
from socf.server.drivers import driver_repo
from socf.services import service_repo

class ConfigurationContext:
    
    def __init__(self, conf_root_path):
        self.__conf_root_path = conf_root_path
    
    def getConfRootPath(self):
        return self.__conf_root_path

class ServiceRegistry:
    
    def __init__(self,  conf_context):
        registry_file_path = os.path.join(conf_context.getConfRootPath(),  "services.xml")
        xmldoc = minidom.parse(registry_file_path)
        self.__serviceMap = {}
        entries = xmldoc.getElementsByTagName("service")
        for entry in entries:
            type = int(entry.getElementsByTagName("service-type")[0].firstChild.data)
            desc = entry.getElementsByTagName("service-description")[0].firstChild.data            
            module = entry.getElementsByTagName("service-module")[0].firstChild.data
            classType = self.__getClassByType(module)
            self.__serviceMap[type] = (classType,  desc)

    def __getClassByType(self,  name):
        module = sys.modules["socf.services.service_repo"]
        class_type = getattr(module,  name)
        return class_type

    def getServiceInfoByType(self,  type):
        return (self.__serviceMap[type][0](),  self.__serviceMap[type][1])

class DeviceTypeRegistry:
    
    def __init__(self,  conf_context):
        registry_file_path = os.path.join(conf_context.getConfRootPath(),  "device_types.xml")
        xmldoc = minidom.parse(registry_file_path)
        self.__deviceTypeList = []
        entries = xmldoc.getElementsByTagName("device")
        for entry in entries:
            type = int(entry.getElementsByTagName("device-type")[0].firstChild.data)
            name = entry.getElementsByTagName("device-name")[0].firstChild.data            
            desc = entry.getElementsByTagName("device-description")[0].firstChild.data
            self.__deviceTypeList.append(DeviceTypeInfo(type,  name,  desc))
    
    def getDeviceTypeList(self):
        return self.__deviceTypeList
    
    
class DeviceTypeInfo:
    
    def __init__(self,  type,  name,  description):
        self.__type = type
        self.__name = name        
        self.__description = description

    def getType(self):
        return self.__type
    
    def getName(self):
        return self.__name

    def getDescription(self):
        return self.__description        


class DeviceDriverMappings:
    
    def __init__(self,  conf_context):
        registry_file_path = os.path.join(conf_context.getConfRootPath(),  "device_driver_mappings.xml")
        xmldoc = minidom.parse(registry_file_path)
        self.__deviceDriverMap = {}
        entries = xmldoc.getElementsByTagName("device-driver")
        for entry in entries:
            type = int(entry.getElementsByTagName("device-type")[0].firstChild.data)
            module = entry.getElementsByTagName("driver-module")[0].firstChild.data
            desc = entry.getElementsByTagName("driver-description")[0].firstChild.data
            classType = self.__getClassByType(module) 
            self.__deviceDriverMap[type] = (classType,  desc)
    
    def getDriverInfoByDevice(self,  device):
        return (self.__deviceDriverMap[device[0]][0](device[1],  device[2]),  self.__deviceDriverMap[device[0]][1])

    def __getClassByType(self,  name):
        module = sys.modules["socf.server.drivers.driver_repo"]
        class_type = getattr(module,  name)
        return class_type

class DriverServiceMappings:
    
    def __init__(self,  conf_context):
        registry_file_path = os.path.join(conf_context.getConfRootPath(),  "driver_service_mappings.xml")
        xmldoc = minidom.parse(registry_file_path)
        self.__driverServiceMap = {}
        entries = xmldoc.getElementsByTagName("driver-service")
        for entry in entries:            
            driver = entry.getElementsByTagName("driver-module")[0].firstChild.data
            type = int(entry.getElementsByTagName("service-type")[0].firstChild.data)
            self.__driverServiceMap[driver] = type;

    def getServiceTypeByDriver(self,  driverName):
        return self.__driverServiceMap[driverName]
